//
//  WebViewController.swift
//  yunshang
//
//  Created by Wei Zhang on 2019/6/2.
//  Copyright © 2019 zw. All rights reserved.
//

import UIKit
import WebKit
class WebViewController: BaseViewController,WKUIDelegate, WKNavigationDelegate {
    
    var webView : WKWebView!
    var url : String = ""
    var htmlString : String = ""
    lazy private var progressView: UIProgressView = {
        self.progressView = UIProgressView.init()
        self.progressView.tintColor = blueColor      // 进度条颜色
        self.progressView.trackTintColor = UIColor.white // 进度条背景色
        return self.progressView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        webView = WKWebView.init()
        self.view.addSubview(webView)
        self.view.addSubview(progressView)
        webView.snp.makeConstraints { (make) in
            make.top.bottom.left.right.equalTo(self.view)
        }
        let top = statusbarHeight + (self.navigationController?.navigationBar.frame.size.height)!
        progressView.snp.makeConstraints { (make) in
            make.left.right.equalTo(self.view)
            make.top.equalTo(top)
            make.height.equalTo(2)
        }
        if htmlString != ""{
            webView.loadHTMLString(htmlString, baseURL: nil)
            self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
        }else if let URL = URL.init(string: url){
            webView.load(URLRequest.init(url: URL))
            self.webView.uiDelegate = self
            self.webView.navigationDelegate = self
            self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
        }
        
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        //  加载进度条
        if keyPath == "estimatedProgress"{
            progressView.alpha = 1.0
            progressView.setProgress(Float((self.webView?.estimatedProgress) ?? 0), animated: true)
            if (self.webView?.estimatedProgress ?? 0.0)  >= 1.0 {
                UIView.animate(withDuration: 0.3, delay: 0.1, options: .curveEaseOut, animations: {
                    self.progressView.alpha = 0
                }, completion: { (finish) in
                    self.progressView.setProgress(0.0, animated: false)
                })
            }
        }
    }
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!){
        
    }
    
    func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!){
        
    }
    func webViewDidClose(_ webView: WKWebView) {
        
    }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
    }
    deinit {
        self.webView.removeObserver(self, forKeyPath: "estimatedProgress")
        self.webView.uiDelegate = nil
        self.webView.navigationDelegate = nil
    }

}
